/*
 * @Author: 李石
 * @Date: 2024-04-13 15:03:30
 * @LastEditors: lishi
 * @LastEditTime: 2024-07-16 09:57:55
 * @Description: 
 * Copyright (c) 2024 by ${lishi0105@163.com}, All Rights Reserved. 
 */

#include <vector>
#include <iostream>
#include <unistd.h>
#include <chrono>
#include "NaluDebug.h"
#include "FileParse.hpp"
#include "StreamParse.hpp"

int main(int argc, char* argv[])
{
    videoinfo_t m_cVideoInfo;
    parse_info("argc: %d\n", argc);
    std::string input = "/home/lishi/video/hkcamera-1080p25-4mb.265";
    if(argc > 1){
        input = std::string(argv[1]);
    }
    FileParse::Ptr parser = FileParse::CreateShared(input.c_str());
    parser->getVideoInfo(&m_cVideoInfo);
    parse_info("[%s] type: %d nalu cout: %lu width:%d, height:%d fps: %.2f\n",input.c_str(),
            m_cVideoInfo.type, 
            m_cVideoInfo.nalu_count, 
            m_cVideoInfo.width,
            m_cVideoInfo.height,
            m_cVideoInfo.fps);
    StreamParse::Ptr streamParse = StreamParse::CreateShared(m_cVideoInfo.type);
    // FILE *fp = fopen("dst.hdr", "wb");
    // for(size_t i = 0; i < m_cVideoInfo.count; i++){
    //     char buffer[128] = {0};
    //     NALU_t nalu_;
    //     parser.getNaluInfo(i, &nalu_);
    //     auto count = snprintf(buffer, sizeof(buffer), "%u %u\n", nalu_.offset, nalu_.len);
    //     fwrite(buffer, 1, count, fp);
    //     fflush(fp);
    // }
    // fclose(fp);
    // return 0;
    unsigned char frame[512*1024] = {0};
    for(size_t i = 0; i < m_cVideoInfo.nalu_count; i++){
        size_t size = parser->getNaluData(i, frame, sizeof(frame));
        if(size == 0){
            continue;
        }
        int32_t start_len = 0;
        auto configFrame_  = _configFrame(m_cVideoInfo.type, frame, size, &start_len);
        auto keyFrame_     = _keyframe(m_cVideoInfo.type, frame, size, &start_len);
        auto dropAble_     = _dropAble(m_cVideoInfo.type, frame, size, &start_len);
        auto decodeAble_   = _decodeAble(m_cVideoInfo.type, frame, size, &start_len);
        if(streamParse->parseStream(frame, size)){
            videoinfo_t info;
            streamParse->getVideoInfo(&info);
            parse_info("width:%d, height:%d fps:%.2f\n", info.width, info.height, m_cVideoInfo.fps);
        }
        usleep(2*1000);
    }
    parse_info("end !\n");
    return 0;
}
